package com.webex.ta.hydra.gui.table.cellrenderer;

import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.core.KeywordRegistry;
import com.webex.ta.hydra.core.ParamDescriptor;
import com.webex.ta.hydra.exceptions.IllegalKeywordException;
import com.webex.ta.hydra.gui.table.DefaultCellBorder;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-19
 * Time: 10:08:51
 */
public class HydraDefaultTableCellRenderer extends DefaultTableCellRenderer {

    private Color gridcolor;

    private static final Color DEBUG_BG = new Color(99, 130, 191);

    private static final Font hint_font = new Font("Courier", Font.PLAIN, 9);

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        gridcolor = table.getGridColor();
        Command cmd = (Command) value;
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (!cmd.isEnabled())
            this.setForeground(Color.gray);

        if (cmd.getValidationInfo(column - 1) != null)
            this.setBorder(new DefaultCellBorder(true, gridcolor));
        else
            this.setBorder(new DefaultCellBorder(false, gridcolor));

        String param = cmd.getParameter(column - 2);
        this.setText(param);

        try {
            ParamDescriptor[] descriptors = KeywordRegistry.getInstance().getParamDescriptors(cmd.getKeyword(), cmd);
            if (descriptors != null && column - 2 >= 0 && column - 2 < descriptors.length) {
                if (param == null || param.equals("")) {
                    this.setText(descriptors[column - 2].getDescription());
                    this.setFont(hint_font);
                    this.setForeground(Color.gray);
                }
                this.setToolTipText(descriptors[column - 2].getDescription());
            }
        } catch (IllegalKeywordException e) {
        }


        if (cmd.isInDebug()) {
            this.setBackground(DEBUG_BG);
        }
        return this;
    }

    @Override
    public void paint(Graphics g) {
        Dimension size = this.getSize();
        int x = size.width;
        int y = size.height;
        super.paint(g);
        g.setColor(gridcolor);
        g.drawRect(0, 0, x - 1, y - 1);
    }
}
